package com.study.async.config;

import com.study.async.anno.Loggable;
import lombok.extern.slf4j.Slf4j;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

import java.lang.reflect.Method;

/**
 * @Author Outsider
 * @Date 2019/10/27 22:00
 * @Version 1.0
 **/
@Slf4j
public class LoggableInterceptor implements MethodInterceptor {

    @Override
    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
        Method method = methodInvocation.getMethod();
        if (method.isAnnotationPresent(Loggable.class)) {
            log.info("T:{},METHOD:{},PARAMS:{}",
                    Thread.currentThread().getName(),
                    method.getName(),
                    methodInvocation.getArguments());
            Object result = methodInvocation.proceed();
            log.info("T:{},METHOD:{},PARAMS:{},RESULT:{}",
                    Thread.currentThread().getName(),
                    method.getName(),
                    methodInvocation.getArguments(),
                    result);
            return result;
        }
        return methodInvocation.proceed();
    }
}
